package com.zf.study.entity;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientInfo {

    private  Socket socket;

    private  PrintWriter writer;

    private  String tag;

    private  long lastResponseTime;

    private String username;

    public ClientInfo(String tag, PrintWriter writer, Socket socket) {
        this.tag = tag;
        this.writer = writer;
        this.socket = socket;
        this.lastResponseTime = System.currentTimeMillis();
    }

    public Socket getSocket() {
        return socket;
    }

    public PrintWriter getWriter() {
        return writer;
    }

    public String getTag() {
        return tag;
    }

    public long getLastResponseTime() {
        return lastResponseTime;
    }

    public void setLastResponseTime(long lastResponseTime) {
        this.lastResponseTime = lastResponseTime;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public void setWriter(PrintWriter writer) {
        this.writer = writer;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void close(){
        try {
            if(writer != null){
                writer.close();
            }
            if (socket != null && !socket.isClosed()) {
                socket.close();
            }
        } catch (IOException e) {
            System.err.println("关闭 socket 时出错: " + e.getMessage());
        }
    }
}
